package com.example.hbuildble

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.hbuildble.databinding.RowDeviceBlueBinding
import com.longtxt.powerband.core.BleDevice

class BleDeviceAdapter(private val callBack:(BleDevice)-> Unit): RecyclerView.Adapter<BleDeviceAdapter.ViewHolder>() {

    private var listItems = listOf<BleDevice>()



    @SuppressLint("NotifyDataSetChanged")
    fun setDataList(list: List<BleDevice>) {
        listItems = list
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        return ViewHolder(RowDeviceBlueBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder:ViewHolder, position: Int) {
        holder.bind(listItems[position])
    }

    override fun getItemCount(): Int {
        return listItems.size
    }

    inner class ViewHolder(private val binding: RowDeviceBlueBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: BleDevice) {
            binding.txtDevice.text = item.name
            binding.txtMac.text = item.mac
            binding.root.setOnClickListener{
                callBack(item)
            }
        }
    }

}